/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.servlet;

import com.qdu.entity.User;
import com.qdu.service.UserService;
import com.qdu.service.impl.UserServiceImpl;
import com.qdu.util.Sendmail;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SendCodeServlet extends HttpServlet {

    private UserService us = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //接收用户请求，封装成对象
            String username = request.getParameter("username");
            String email = request.getParameter("useremail");
            User User = new User(username, email);
            User resetUser = us.getResetCode(User);
            //用户注册成功之后，给用户发送一封邮件
            //我们使用线程来专门发送邮件，防止出现耗时，和网站注册人数过多的情况；
            Sendmail send = new Sendmail(resetUser);
            //启动线程，线程启动之后就会执行run方法来发送邮件
            send.start();
            if (resetUser != null) {
                //注册用户
                request.setAttribute("message", "注册成功，我们已经发了一封带了注册信息的电子邮件，请查收！如网络不稳定，可能过会儿才能收到！！");
                request.getRequestDispatcher("/resetPwdEnd.jsp").forward(request, response);
            } else {
                request.setAttribute("message", "请检查当前用户名与绑定的邮箱是否一致！！");
                request.getRequestDispatcher("/resetPwd.jsp").forward(request, response);
            }

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "请检查当前用户名与绑定的邮箱是否一致！！");
            request.getRequestDispatcher("/resetPwd.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
